package com.jh.readme.iu.fragment.picture;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jh.readme.R;
import com.jh.readme.iu.fragment.news.common.MyTabItem;
import com.jh.readme.iu.fragment.news.common.SimpleFragmentPagerAdapter;
import com.jh.readme.iu.fragment.news.entity.NewsEntity;
import com.jh.tools.api.Api;
import com.jh.tools.base.BaseFragment;
import com.jh.tools.bean.SinaPhotoList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *   图片页
 */
public class PictureFragment extends BaseFragment<PicturePresenter,PictureModel> implements PictureContract.View {

    TabLayout mTabLayout;// 类别标签
    ViewPager mViewPager;//分类
    private List<NewsEntity> mNewsEntitys;
    private SimpleFragmentPagerAdapter pagerAdapter;
    MyTabItem[] tabs;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView(View baseView) {
        mTabLayout=(TabLayout) baseView.findViewById(R.id.tab_news_title);
        mViewPager=(ViewPager) baseView.findViewById(R.id.news_content);
        mNewsEntitys=new ArrayList<>();
        List<String> channelName = Arrays.asList(this.getResources()
                .getStringArray(R.array.img_channel));

        List<String> channelId = Arrays.asList(this.getResources()
                .getStringArray(R.array.img_channel_id));
        for (int i = 0; i <3 ; i++) {
            NewsEntity newsEntity=new NewsEntity();
            newsEntity.tabNewsName=channelName.get(i);
            newsEntity.newsId= channelId.get(i);
            newsEntity.newsType= Api.getType(channelId.get(i));
            mNewsEntitys.add(newsEntity);
        }
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel,this);
    }

    @Override
    public void initTab(List<NewsEntity> baseFragments) {
        pagerAdapter = new SimpleFragmentPagerAdapter(this.getFragmentManager(),mNewsEntitys, this.getActivity());
        mViewPager.setAdapter(pagerAdapter);

        mTabLayout.setupWithViewPager(mViewPager);

        tabs = new MyTabItem[mTabLayout.getTabCount()];
        int size=mTabLayout.getTabCount();
        for (int i = 0; i < size; i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tabs[i] = new MyTabItem(this.getContext(),mNewsEntitys.get(i).tabNewsName);
            tab.setCustomView(tabs[i].getTabView());
//            tab_news_title.addTab(tab_news_title.newTab().setText(mNewsEntitys.get(i).tabNewsName));
        }

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabs[position].setIndicVisibility(3);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
